package cn.comtom.ewbs.signature.common.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import cn.comtom.ewbs.signature.service.AbstractSignService;
import cn.comtom.ewbs.signature.service.ISignatureService;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 标准 Servlet 过滤器，实现 javax.servlet.Filter 接口，并重现它的 3 个方法
 * filterName：表示过滤器名称，可以不写
 * value：配置请求过滤的规则，如 "/*" 表示过滤所有请求，包括静态资源，如 "/user/*" 表示 /user 开头的所有请求
 */
@WebFilter(filterName = "SignFilter", value = {"/*"})
@Slf4j
public class SignFilter implements Filter {
	
	@Autowired
	private ISignatureService signatureService;
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    	if(StrUtil.isBlank(AbstractSignService.signType)) {
        	boolean initResult = signatureService.initSignDev(null);
        	log.info("初始化签名设备:{}",initResult?"成功":"失败");
        }
    	filterChain.doFilter(servletRequest, servletResponse);
    }
 
   
}

